Skip to content

Conversation

Crell
Copy link
Contributor

@Crell Crell commented Sep 24, 2025

This is the simplest attribute I can think of, so it should serve as a way for us to flesh out our process, format, etc. It's borrowed from the PHPStorm attributes (that I don't know anyone used).

The criteria of a pure function I copied directly from Wikipedia. The rest is my own writing. All subject to review, naturally.

Copy link
Member

@jaapio jaapio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think it's clear what the intention is, I do have some suggestions to make it more clear

* 4. Read from an object property on the same object unless that property is `readonly`.
* 5. Call any function that is not also marked "pure." (It may invoke a callable passed to it as an explicit argument.)
*
* Many functions in the PHP standard library are already pure. Implementers MUST treat those as acceptable to call
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this more explicit somehow? Like a reference of functions? Right now it sounds like, you need to figure out yourself if functions are pure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list is... not small. 90% of the string and array functions would fall into this category. Listing them here would be a challenge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 Options IMO:

Comment on lines +17 to +18
* In PHP, that means a pure function MUST NOT:
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to reread this part before I noticed that the Must not was a list of prohibited operations for pure functions.
Maybe we should repeat the "must not" on every line rather than just once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good. It also.allows one to mix requirements like

  • MUST NOT do x
  • SHALL do y
  • but MUST do that

@jrfnl
Copy link
Contributor

jrfnl commented Sep 27, 2025

I'm missing what the added value is of marking a function as Pure and what the attribute is supposed to do in this case (well, it has a tiny bit of info in the last sentence, but I don't think that's enough, nor the right place for that info).

@Crell
Copy link
Contributor Author

Crell commented Sep 28, 2025

The use case would be for telling an SA tool "I expect this entire stack of function calls to be pure, and have no side effects. Make sure I'm correct."

@jrfnl
Copy link
Contributor

jrfnl commented Sep 28, 2025

The use case would be for telling an SA tool "I expect this entire stack of function calls to be pure, and have no side effects. Make sure I'm correct."

Couple of questions/remarks:

  • I think the reason/use-case for an attribute/when to use it should probably be the very first thing in the long description.
  • Could the attribute do any verification of the function actually being "pure" ?
    • If so, I would expect that to be part of the attribute in this repo.
    • If not, what would be the argument for using an attribute for this over an annotation ?

*
* If this attribute is placed on a method in an interface, it means that all implementations of that method MUST
* themselves be marked pure. Implementers MUST enforce this rule.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a reference to where the information came.from so that people realize that this is not something we invented ourselves but is based on other peoples know-how.

In this case a link to the Wikipedia page seems to be a good idea.

Using either a1 or an {@ see} Annotation (oh the irony 😂)

Footnotes

  1. footnote

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to add the requirement of references to the meta-document....

@heiglandreas
Copy link
Contributor

It looks like creating a template for a new attribute might be a good idea to provide some guidelines of what to think of and how to structure the information so that nothing important gets lost and the informations are roughly compareable.

@Crell
Copy link
Contributor Author

Crell commented Sep 29, 2025

Indeed. The main point of this attribute is/was to give us a concrete example to chew on to develop said template. 😄

What sections do you see, and what format? Like, should we use Markdown in the docblock?

@heiglandreas
Copy link
Contributor

Markdown is.most widely used, so that would probably be the most preferred option.

I'd though prefere Restructured Text as it is much more feature-rich.

If I recall correctly PHPDocumentor as well as GitHub can render ReST as well as Markdown, so this seems to be a technicallity.

I'll try to start a template tomorrow in a separate PR


/**
* Indicates that a function or method is a "pure" function.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (also to consider for the template):

Suggested change
*
*
* Target audience:
* - Implementors: static analysis tools.
* - Users: packages which want to safeguard "pure" functions via static analysis tools.

Phrasing may need work, but hopefully you get my drift.

@jaapio
Copy link
Member

jaapio commented Sep 30, 2025

If I recall correctly PHPDocumentor as well as GitHub can render ReST as well as Markdown, so this seems to be a technicallity.

phpDocumentor can render ReST, only for documents. Docblocks must be written in commonmark markdown. It has been like that for years. And most tooling is working like that. So if we want to add some structure it must be in markdown.

@heiglandreas
Copy link
Contributor

phpDocumentor can render ReST, only for documents. Docblocks must be written in commonmark markdown. It has been like that for years. And most tooling is working like that. So if we want to add some structure it must be in markdown.

Thanks for the input. Rewrote the ReST template in Markdown now 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants